package attack.snowman;

import java.awt.geom.Rectangle2D;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Point;
import org.newdawn.slick.geom.Rectangle;

import framework.BasicFighter;
import framework.Util;
import framework.movements.LinearMovement;
import attack.arfok.Tornado;

public class SnowRoll extends Tornado {

	protected Image stop;
	protected Image stopBackward;
	protected Image stopUsed;

	protected boolean cast;

	public SnowRoll(BasicFighter attacker) {
		super(attacker);
		this.spriteSheet = attacker.getAnimations().getSpriteSheet();
		this.attackAnimation = new Animation();
		//this.attackAnimation.addFrame(this.spriteSheet.getSprite(2, 1), 150);
		this.attackAnimation.addFrame(this.spriteSheet.getSprite(2, 1), 200);
		this.attackAnimation.addFrame(this.spriteSheet.getSprite(4, 2), 100);
		this.attackAnimation.addFrame(this.spriteSheet.getSprite(0, 3), 100);
		this.attackAnimation.addFrame(this.spriteSheet.getSprite(1, 3), 100);
		this.attackAnimationBackward = Util.getFlippedAnimation(this.attackAnimation, true, false);
		this.attackAnimation.setAutoUpdate(false);
		this.attackAnimationBackward.setAutoUpdate(false);
		this.attackAnimationUsed = this.attackAnimation;

		this.stop = this.spriteSheet.getSprite(3, 2);
		this.stopBackward = this.stop.getFlippedCopy(true, false);

		this.isJumpingAuthorized = true;
		cast = false;
	}


	@Override
	public void trigger() {
		super.trigger();
		cast = false;
		windUp = 400;
	}

	@Override
	public void draw(Graphics g) {
		BasicFighter a = getAttacker();
		Color filter = null;
		if (attackAnimationUsed.getFrame() == 0 && cast) {
			this.attackAnimationUsed.setCurrentFrame(1);
		}


		if (this.currentTime > getDuration() * 0.6f  && (currentTime / 150) % 2 == 0) {
			filter = new Color(160, 160, 160, 255);
		}

		if (cast && (a.isJumping() || a.getXspeed() == 0)) {
			this.stop.draw(a.getX(), a.getY(), a.getWidth(), a.getHeight(), filter);
		} else {
			this.attackAnimationUsed.draw(a.getX(), a.getY(), a.getWidth(), a.getHeight(), filter);
		}
	}

	@Override
	public Rectangle getHitBox() {
		Rectangle hitBox, att;
		int width, // attack width
		height, // attack height
		hitboxY, // x position of the hitbox
		hitboxX; // y position of the hitbox

		att = getAttacker().getHitBox();
		width = (int) (att.getWidth() * 1.5f);
		height = (int) (att.getHeight() / 3);
		hitboxY = (int) (att.getY() +  att.getHeight() / 2);
		hitboxX = (int) (att.getCenterX() - width / 2);

		hitBox = new Rectangle(hitboxX, hitboxY, width, height);
		return attackAnimationUsed.getFrame() > 0 ? 
				hitBox : null;
	}

	@Override
	public void update(GameContainer gc, int delta) {
		super.update(gc, delta);
		BasicFighter a = getAttacker();
		cast |= attackAnimationUsed.getFrame() > 0;

		if (cast && a.isMoving()) {
			attackAnimationUsed = a.getXspeed() < 0 ? attackAnimationBackward : attackAnimation;
		} else {
			attackAnimationUsed = a.isForward() ? attackAnimation : attackAnimationBackward;
		}

		if (attackAnimationUsed.getFrame() > 0 &&  (a.isJumping() && currentTime < 150 
				|| a.getCurrentJump() >= a.getHitBox().getHeight() * 2)) {
			a.setYspeed(Math.abs(a.getYspeed()));
		} 

		if (cast && (a.isJumping() || !a.isMoving() || attackAnimationUsed.getFrame() == 0 && cast)) {
			attackAnimationUsed.setCurrentFrame(1);
		} else {
			attackAnimationUsed.update(delta);
		}

		// adding some speed
		if (!a.isJumping() && cast && !getAttacker().getMove().isActive()) {
			getAttacker().setX(a.getX() + a.getStepXSpeed(delta));
		}
	}

	@Override
	public boolean isBlocking() {
		System.out.println(getAttacker().getYspeed());
		return attackAnimationUsed.getFrame() == 0 && isActive();
	}

	@Override
	public void onHit(BasicFighter f, Rectangle2D Collision) {
		BasicFighter a = getAttacker();
		int forward = a.isForward() ? 1 : -1;
		if (a.isMoving()) {
			if (!hasHit) {
				LinearMovement m = new LinearMovement(a, new Point(a.getX() - (a.getHeight() / 2 * forward), a.getY() - a.getHeight() / 6));
				m.setDuration(300);
				a.move(m);
				if (a.getYspeed() < 0) {
					m = new LinearMovement(f, 100 * forward, -100);
				} else {
					m = new LinearMovement(f, 130 * forward, -30);
				}
				m.setDuration(300);
				f.move(m);
			}
			super.onHit(f, Collision);
		}
	}	

	@Override
	public void onRelease() {
		interrupt();
	}

	@Override
	public int getPower() {
		return 10;
	}

	@Override
	public int getDuration() {
		return 3000;
	}
}
